package com.sgb.kjwl.viewmodel.enterprise;

import android.app.Application;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.google.gson.Gson;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.EnterpriseAPIManager;
import com.swgk.core.base.api.ICallBack;
import com.sgb.kjwl.model.entity.NeedDetailsEntity;
import com.sgb.kjwl.model.entity.enterprise.AttentionEntity;
import com.sgb.kjwl.model.entity.enterprise.ListBean;
import com.sgb.kjwl.model.entity.enterprise.NeedDetailSignUpEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.util.UserOperating;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.util.SharedPreferenceUtil;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class NeedDetailsViewModel_New extends AndroidViewModel {

    public MutableLiveData<String> Toast_until = new MutableLiveData<>();
    public MutableLiveData<Integer> ShowLoading = new MutableLiveData<>();
    public MutableLiveData<NeedDetailsEntity> date_detail = new MutableLiveData<>();//详情
    public MutableLiveData<List<ListBean>> list_recomend = new MutableLiveData<>();//推荐数据
    public String mitemid;
    public String[] titleList = {"需求信息", "报价要求", "报价单位"};
    public MutableLiveData<Integer> isgz = new MutableLiveData<>();
    public MutableLiveData<String> dataid = new MutableLiveData<>();
    public MutableLiveData<Boolean> follow_company = new MutableLiveData<>();
    public MutableLiveData<Boolean> goQuote = new MutableLiveData<>();

    public String companyId = "";

    public MutableLiveData<List<NeedDetailSignUpEntity>> list_signup = new MutableLiveData<>();

    public NeedDetailsViewModel_New(@NonNull @NotNull Application application) {
        super(application);
    }

    public void setMitemid(String id) {
        this.mitemid = id;
    }

    public void getDetails() {
        ShowLoading.postValue(1);
        APIManager.getInstance().getNeedAPI().getDetails(mitemid).enqueue(new Callback<BaseEntity<NeedDetailsEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<NeedDetailsEntity>> call, Response<BaseEntity<NeedDetailsEntity>> response) {
                ShowLoading.postValue(2);
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    date_detail.postValue(response.body().getData());
                } else {
                    Toast_until.postValue(response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<NeedDetailsEntity>> call, Throwable t) {
                ShowLoading.postValue(2);
                Toast_until.postValue(t.getMessage());

            }
        });
    }


    /**
     * 关注
     *
     * @param map
     * @param isGz
     */
    public void focusNeed(Map<String, Object> map, int isGz) {
        ShowLoading.postValue(1);
        APIManager.getInstance().getEnterpriseAPI().saveAttention(map).enqueue(new Callback<BaseEntity<String>>() {
            @Override
            public void onResponse(Call<BaseEntity<String>> call, Response<BaseEntity<String>> response) {
                ShowLoading.postValue(2);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {

                    if (response.body().getData().contains("下架")) {
                        Toast_until.postValue(response.body().getData());
                    } else {
                        dataid.postValue(String.valueOf(response.body().getData()));
                        isgz.postValue(isGz);
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<String>> call, Throwable t) {
                ShowLoading.postValue(2);
            }
        });
    }

    public void getAttentionStatus(String userNo, int type, String dataId) {
        ShowLoading.postValue(1);
        APIManager.getInstance().getEnterpriseAPI().getAttentionInfoByData(userNo, type, mitemid).enqueue(new Callback<BaseEntity<List<AttentionEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AttentionEntity>>> call, Response<BaseEntity<List<AttentionEntity>>> response) {
                ShowLoading.postValue(2);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() != null && response.body().getData().size() > 0) {//已关注
                        isgz.postValue(0);
                        dataid.postValue(String.valueOf(response.body().getData().get(0).getId()));
                    } else {
                        isgz.postValue(1);
                    }

                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AttentionEntity>>> call, Throwable t) {
                ShowLoading.postValue(2);
            }
        });
    }

    /**
     * 推荐需求列表
     *
     * @param category
     * @param id
     */
    public void getRecommendList(String type, String category, String id) {
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getServiceNeedRecommendlist_new(category, 6, type, id).enqueue(new Callback<BaseEntity<List<ListBean>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<ListBean>>> call, Response<BaseEntity<List<ListBean>>> response) {
                ShowLoading.postValue(2);
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    list_recomend.postValue(response.body().getData());
                } else {
                    Toast_until.postValue(response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<ListBean>>> call, Throwable t) {
                ShowLoading.postValue(2);
            }
        });
    }

    /**
     * 推荐服务需求列表
     *
     * @param categoryFirstId
     * @param id
     */
    public void getServiceRecommendList(String categoryFirstId, String id) {
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getServiceNeedRecommendlist_new(categoryFirstId, id).enqueue(new Callback<BaseEntity<List<ListBean>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<ListBean>>> call, Response<BaseEntity<List<ListBean>>> response) {
                ShowLoading.postValue(2);
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    list_recomend.postValue(response.body().getData());
                } else {
                    Toast_until.postValue(response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<ListBean>>> call, Throwable t) {
                ShowLoading.postValue(2);
            }
        });
    }


//    //-----用户查询是否关注企业----------------------------------------------------------------
//    public void getUserFocusCompanyStatus() {
//        if (TextUtils.isEmpty(companyId)) {
//            return;
//        }
//        ShowLoading.postValue(1);
//        Map<String, String> map = new HashMap();
//        map.put("siteNo", companyId);
//        map.put("siteType", 1 + "");
//        APIManager.getInstance().getWebsiteInfoAPI().getUserFocusCompanyStatus(map).enqueue(new ICallBack<BaseEntity>() {
//            @Override
//            public void onSuccess(BaseEntity data) {
//                ShowLoading.postValue(2);
//                if (data.getState().equals(APIManager.RESPONSE_OK)) {
//
//                }
//            }
//        });
//
//    }

    //-----关注状态----------------------------------------------------------------
    public void attentionCompanyStatus() {
        if (TextUtils.isEmpty(companyId)) {
            return;
        }
        ShowLoading.postValue(1);
        Map<String, String> map = new HashMap();
        map.put("businessNo", companyId);
        map.put("type", 0 + "");
        Log.e("message",new Gson().toJson(map));
        APIManager.getInstance().getEnterpriseAPI().userFocusCompany(map).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                ShowLoading.postValue(2);
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    follow_company.postValue(!follow_company.getValue());
                } else {
                    Toast_until.postValue(data.getMsg());
                }
            }
        });
    }


    /**
     * 专利状态判定
     *
     * @param mpatentStatus
     * @return
     */
    public String getStringPatentStatus(String mpatentStatus) {

        if (TextUtils.isEmpty(mpatentStatus)) {
            return "";
        }
        StringBuilder patentStatusStr = new StringBuilder();
        if (mpatentStatus.indexOf(",") > -1) {
            for (String patentStatus : mpatentStatus.split(",")) {
                if (patentStatus.equals("1")) {
                    patentStatusStr.append("授权已下证、");
                }
                if (patentStatus.equals("0")) {
                    patentStatusStr.append("授权未下证、");
                }
            }
            patentStatusStr = new StringBuilder(patentStatusStr.substring(0, patentStatusStr.length() - 1));
        } else {
            patentStatusStr = new StringBuilder(mpatentStatus.contains("0") ? "授权未下证" : mpatentStatus.contains("1") ? "授权已下证" : "");
        }

        return patentStatusStr.toString();
    }


    public String getBjType(int offerStatus) {
        String datta = "/";
        switch (offerStatus) {
            case 3:
                datta = "已取消";
                break;
            case 1:
                datta = "报价中";
                break;
            case 2:
                datta = "已结束";
                break;
        }

        return datta;
    }


    //1商标 2专利 3版权 4 公司 5 服务 6 资产
    public String getneedType(int requirementTypes) {
        String data = "/";
        switch (requirementTypes) {
            case 1:
                data = "商标需求";
                break;
            case 2:
                data = "专利需求";
                break;
            case 3:
                data = "版权需求";
                break;
            case 4:
                data = "公司需求";
                break;
            case 5:
                data = "服务需求";
                break;
            case 6:
                data = "资产需求";
                break;
        }

        return data;
    }


    /**
     * 授权方式
     *
     * @param mcopyrightAuthorization
     * @return
     */
    public String getStringcopyrightAuthorization(String mcopyrightAuthorization) {
        if (TextUtils.isEmpty(mcopyrightAuthorization)) {
            return "";
        }
        StringBuilder copyrightAuthorizationStr = new StringBuilder();
        if (mcopyrightAuthorization.indexOf(",") > -1) {
            for (String copyrightAuthorization : mcopyrightAuthorization.split(",")) {
                copyrightAuthorizationStr.append(copyrightAuthorization.contains("1") ? "独家授权、" : "非独家授权、");
            }
            copyrightAuthorizationStr = new StringBuilder(copyrightAuthorizationStr.substring(0, copyrightAuthorizationStr.length() - 1));
        } else {
            copyrightAuthorizationStr = new StringBuilder(mcopyrightAuthorization.contains("1") ? "独家授权" : "非独家授权");
        }

        return copyrightAuthorizationStr.toString();
    }

    /**
     * 服务分类
     *
     * @param class_type1
     * @param class_type2
     * @param class_type3
     * @return
     */
    public String getStringServiceType(String class_type1, String class_type2, String class_type3) {
        String service_Type = "/";
        if (!TextUtils.isEmpty(class_type1)) {
            service_Type = class_type1;
        }
        if (!TextUtils.isEmpty(class_type2)) {
            service_Type = service_Type + "-" + class_type2;
        }
        if (!TextUtils.isEmpty(class_type3)) {
            service_Type = service_Type + "-" + class_type3;
        }
        return service_Type;
    }


    public void getsignuplist(String id) {

        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getSignUpList(id, 1+"", 4).enqueue(new Callback<BaseEntity<ListResponse<List<NeedDetailSignUpEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<NeedDetailSignUpEntity>>>> call, Response<BaseEntity<ListResponse<List<NeedDetailSignUpEntity>>>> response) {
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData().getList() != null && response.body().getData().getList().size() > 0) {
                        list_signup.postValue(response.body().getData().getList());
                    }
                } else {
                    Toast_until.postValue(response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<NeedDetailSignUpEntity>>>> call, Throwable t) {
                Toast_until.postValue(t.getMessage());
            }
        });

    }


    /**
     * 是否可以报价
     */
    public void goQuotation() {
        int type = UserOperating.getInstance().getUserIdentity();
        String no = "";
        switch (type) {
            case 0:
                no = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.USER_NO, "");
                break;
            case 1:
            case 2:
                no = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.COMPANY_NO, "");
                break;
        }
        String userAccount = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LOGIN_PHONE, "");
        APIManager.getInstance().getNeedAPI().goQuotation(mitemid, no, String.valueOf(type), userAccount).enqueue(new Callback<BaseEntity<Object>>() {
            @Override
            public void onResponse(Call<BaseEntity<Object>> call, Response<BaseEntity<Object>> response) {
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    goQuote.postValue(true);
                } else if (response.body().getState().equals(APIManager.RESPONSE_FAIL)) {
                    Toast_until.postValue(response.body().getMsg());
//                    getDetails();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<Object>> call, Throwable t) {
                Toast_until.postValue(t.getMessage());

            }
        });
    }


    /**
     * 判定需求是否下架
     */

}


